package com.sczmgk.hotline_pzh.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.ui.SearchActivity;
import com.sczmgk.hotline_pzh.utils.UIUtils;


/**
 * Created by Administrator on 2016/12/6 0006.
 */

public class MySearchListView extends ListView {

    private int downY = -1;

    /*头的测量高度*/
    private int headerMeasuredHeight;

    /*头布局*/
    private View header;

    /*搜索框布局控件*/
    private LinearLayout mLayoutSearch;

    /*搜索框是否显示*/
    private boolean isShow = false;

    private int topPadding;

    public MySearchListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addHeader(context);
    }

    /**
     * 添加头
     */
    private void addHeader(final Context context) {

        //填充布局
        header = View.inflate(getContext(), R.layout.search_bar_header, null);

        //搜索框的控件
        mLayoutSearch = (LinearLayout) header.findViewById(R.id.ll_search);

        // 隐藏头布局
        header.measure(0, 0);
        headerMeasuredHeight = header.getMeasuredHeight();
        header.setPadding(0, -headerMeasuredHeight, 0, 0);
        this.addHeaderView(header);

        //点击搜索框后跳转到搜索界面
        setOnSearchClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.startActivity(context , SearchActivity.class);
            }
        });
    }

    public void setOnSearchClickListener(OnClickListener click) {
        mLayoutSearch.setOnClickListener(click);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:

                downY = (int) ev.getY();

                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) ev.getY();

                if (downY == -1) {
                    downY = (int) ev.getY();
                }

                // 手指移动的距离
                int diffY = moveY - downY;

                // 只有第一个条目完全展示时，才处理事件
                if (getFirstVisiblePosition() != 0) {
                    downY = (int) ev.getY();
                    break;
                }

                // 只处理从上往下滑动
                if (diffY > 0) {

                    if (!isShow) {
                        topPadding = diffY - headerMeasuredHeight;

                        if (topPadding > 0) {
                            topPadding = 0;
                        }

//                    if (topPadding <= 0) {
//                        isShow = true;
//                    }

                        header.setPadding(0, topPadding, 0, 0);
//                        return true;
                    }
                }

                break;
            case MotionEvent.ACTION_UP:

                //如果显示的是第一个条目并且是下拉,搜索框未显示, 那么显示搜索框
                if (getFirstVisiblePosition() == 0) {
                    if (!isShow) {
                        float upY = ev.getY();
                        float diffentY = upY - downY;
                        if (diffentY >= 0) {
                            isShow = true;
                        }
                    }
                }

                // 每一次滑动，downY需要重新赋值
                downY = -1;

                if (isShow) {
                    header.setPadding(0, 0, 0, 0);
                    //延迟5秒关闭
                    closeSearchBar();
                } else {
                    header.setPadding(0, -headerMeasuredHeight, 0, 0);
                }

                break;

            default:
                break;
        }

        return super.onTouchEvent(ev);
    }

    /**
     * 延迟5秒后隐藏搜索框
     */
    private void closeSearchBar() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isShow = false;
                header.setPadding(0, -headerMeasuredHeight, 0, 0);
            }
        }, 5000);
    }


}
